package com.ag.improvedjava.iterable;

import java.util.Comparator;

final class CompositeKey<KP /* Key primary */, KS /* Key secondary */> {

	private final KP primary;
	private final KS secondary;
	
	CompositeKey(KP primary, KS secondary) {
		this.primary = primary;
		this.secondary = secondary;
	}
	
	static class KeyComparator<KP, KS> implements Comparator<CompositeKey<KP, KS>> {

		private final Comparator<KP> primaryComparer;
		private final Comparator<KS> secondaryComparer;
		
		KeyComparator(Comparator<KP> primaryComparer,
				Comparator<KS> secondaryComparer) {
			this.primaryComparer = primaryComparer;
			this.secondaryComparer = secondaryComparer;
		}

		@Override
		public int compare(CompositeKey<KP, KS> k1, CompositeKey<KP, KS> k2) {
			int primaryResult = primaryComparer.compare(k1.primary, k2.primary);
			if (primaryResult != 0) {
				return primaryResult;
			}
			return secondaryComparer.compare(k1.secondary, k2.secondary);
		}
		
	}
	
}
